/*
 * StringRecordReader.cpp
 *
 */

#include "StringRecordReader.h"
#include <sstream>

StringRecordReader::StringRecordReader() {}

StringRecordReader::~StringRecordReader() {}

bool StringRecordReader::open(const String& path) {
	file.open(path.c_str(), std::fstream::in);
	count = 0;
	return file.good();
}

bool StringRecordReader::next(StringRecord& record) {
	String line;
	if (std::getline(file, line)) {
		BigInt separator = line.find_first_of(SEPARATOR, 0);
		record.setId(toBigInt(line.substr(0, separator)));
		record.setValue(line.substr(separator + 1, line.length()));
		count++;
		return true;
	}
	return false;
}

BigInt StringRecordReader::toBigInt(const String& value) const {
	std::stringstream ss(value);
	BigInt val;
	ss >> val;
	return val;
}

int StringRecordReader::getCount()const {
	return count;
}
